<?php
plugin('admin-top-plugins');
global $pluginData;
global $pluginInterfaceList;
if(isset($_GET['interface']) && $admin->canDo('View Plugin Pages')) {
	if(count($pluginData[$_GET['interface']]['name']) < 1)
		$_GET['interface'] = str_replace('-',' ',$_GET['interface']);
	ob_start();
	runInterface($_GET['interface']);
	$output = ob_get_contents();
	ob_end_clean();
	$title = ' : '.($pluginData[$_GET['interface']]['name']);
}
elseif(!$admin->canDo('View Plugin Pages')) {
	$admin->noAccess();
}
else {
if($admin->canDo('Toggle Plugins')) {
	if(isset($_POST['action'])) {
		foreach($_POST as $key => $value) {
			$_POST[$key] = stripslashes($value);
		}
			if($_POST['action'] == 'Activate') {
				$options = serialize(array('active'=>'yes'));
				runInstall($_POST['name']);
			}
			else {
				$options = serialize(array('active'=>'no'));
				runUninstall($_POST['name']);
			}
			$sql = "UPDATE ".DBTABLEPREFIX."plugins SET options = '{$options}' WHERE name = '{$_POST['name']}'";
			$query = $admin->db_query($sql);			
			$numRows = $admin->db->affectedRows($query);
			if($numRows == 0) {
				$sql ="INSERT INTO ".DBTABLEPREFIX."plugins (name, options) VALUES ('{$_POST['name']}', '{$options}')";
				$query = $admin->db_query($sql) or die($admin->message('error', 'MySQL Error', $admin->db->error()));			
			}
			echo $admin->message('success', 'The plugin "'.$_POST['name'].'" was '.strtolower($_POST['action']).'d.', 'If your site fails to load, try deactivating the plugin');
		exit();
	}
	$output .= <<<EOT
		<h2>Plugins</h2>
		<script type="text/javascript">
			$(document).ready(function()  {
				$('.pluginActivate').click(function () {
					var clicked =$(this);
					$('#response').html('').show();
					$.ajax({
						type: 'POST',
						url: 'admin.php?page=plugins',
						data: 'action='+$(clicked).text()+'&name='+$(clicked).attr('title'),
						success: function (responseText) {
						
							$('#response').html(responseText);
							$('#response').animate({opacity:1},2500).slideUp();
							var parent = $(clicked).parent();
							if($(parent).attr('class') == 'Activate') {
								cstr = 'Deactivate';
							}
							else {
								cstr = 'Activate';
							}
							$(parent).attr('class', cstr);
							$(clicked).text(cstr);				
						}
					});
				});
			});
		</script>
		<div id="response">
			
		</div>
		<table id="pluginTable" cellspacing="0" cellborder="0">
			<thead>
				<tr>
					<th>Name</th>
					<th>Description</th>
					<th>Activate</th>
				</tr>
			</thead>
			<tbody>
EOT;
	  function array_filter_recursive($input)
	  {
		foreach ($input as &$value)
		{
		  if (is_array($value))
		  {
			$value = array_filter_recursive($value);
		  }
		}
	   
		return array_filter($input);
	  } 
	  $pluginData = array_filter_recursive($pluginData);
	$sql = "SELECT * FROM ".DBTABLEPREFIX."plugins";
	$query = $admin->db_query($sql) or die($output .= $admin->message('error', 'MySQL Error', $admin->db->error()));
	while($plugin = $admin->db->fetchAssoc($query)) {
		$dbPluginList[$plugin['name']] = array();
		$dbPluginList[$plugin['name']]['options'] = unserialize($plugin['options']);
	}
	function hasUpper($str) {
		$len = strlen($str);
		$prevIsUpper = false;
		for($i = 0; $i < $len; $i++) {
			if(ctype_upper($str[$i]))
				return true;
		}
		return false;
	}
	foreach($pluginData as $key => $value) {
		if(!hasUpper($key) && substr_count(' ', $key) == 0) {
			$name = $value['name'];
			if($dbPluginList[$name]['options']['active'] == "yes") {
				$activateStr = 'Deactivate';
			}
			else {
				$activateStr = 'Activate';
			}
			$output .= <<<EOT
					<tr>
						<td class="pluginName">{$value['name']}<span class="pluginAuthor">{$value['author']}</a></td>
						<td>{$value['desc']}</td>
						<td class="{$activateStr}"><a href="javascript:;" class="pluginActivate" title="{$name}">{$activateStr}</a></td>
					</tr>
EOT;
		}
	}
	$output .= <<<EOT
			</tbody>
		</table>
EOT;
}
}
if(!$admin->canDo('Toggle Plugins')) {
	$admin->noAccess('Toggle Plugins');
}


$admin->page_info['content']['page_title'] = "Plugins".$title;
$admin->page_info['content']['page_content']	= $output;
plugin('admin-bottom-plugins');
//Display page
include(BASE_URL.'includes/admin/admin.php');
?>